package com.wanjia.estate.service.bdservice.bdcdjxxcxjgzmservice;

import cn.hutool.core.util.StrUtil;
import com.wanjia.estate.controller.OtherController;
import com.wanjia.estate.mapper.YtMapper;
import com.wanjia.estate.pojo.bank.form.FormItem;
import com.wanjia.estate.pojo.entity.Bdcdjxxjgcx;
import com.wanjia.estate.pojo.entity.TaoCi;
import com.wanjia.estate.properties.FileTemplateProperties;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ExcelReaderWriterBdc2Service {

    @Autowired
    private FileTemplateProperties fileTemplateProperties;

    @Autowired
    YtMapper mapper;

    @Autowired
    EnquiryOfMinorsService enquiryOfMinorsService;

    @Autowired
    OtherController otherController;

    public String ExcelReaderWriter(List<Bdcdjxxjgcx> bdcdjxxjgcxList, TaoCi taoCi,Bdcdjxxjgcx bdcdjxxjgcx, String folder) {
        try {
            FileInputStream inputStream = new FileInputStream(fileTemplateProperties.getBdcdjxxcxjgzm());
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            Row row2 = sheet.getRow(1);
            row2.getCell(3).setCellValue(bdcdjxxjgcx.getYcslywh());
            row2.getCell(7).setCellValue(bdcdjxxjgcx.getYwlx());

            Row row3 = sheet.getRow(2);
            row3.getCell(3).setCellValue(bdcdjxxjgcx.getCxrlx());

            //添加查询人
            Row row4 = sheet.getRow(3);
            row4.getCell(3).setCellValue(bdcdjxxjgcx.getCxr1());

            Row row5 = sheet.getRow(4);
            row5.getCell(3).setCellValue(taoCi.getXcr());

            List<String> cqzhlist = new ArrayList<>();
            List<String> fwytlist = new ArrayList<>();
            List<String> zllist = new ArrayList<>();
            List<String> gyfslist = new ArrayList<>();

            Optional.ofNullable(bdcdjxxjgcxList)
                    .orElseGet(ArrayList::new)
                    .forEach(b -> {
                        cqzhlist.add(b.getCqzh());
                        fwytlist.add(b.getFwyt());
                        zllist.add(b.getZl());
                        if (StrUtil.isNotBlank(b.getGyfs())) {
                            gyfslist.add(b.getGyfs());
                        }
                    });

            String cqzh = String.join(",", cqzhlist);
            String fwyts = String.join(",", fwytlist);
            String zl = String.join(",", zllist);
            String gyfs = String.join(",", gyfslist);

            //已有房产（查询人+协查人）
            Row row7 = sheet.getRow(6);
            if(cqzh.isEmpty()){
                row7.getCell(1).setCellValue("");
            }else {
                row7.getCell(1).setCellValue(bdcdjxxjgcx.getCxr1()+taoCi.getXcr());
            }
            row7.getCell(3).setCellValue(cqzh);
            //登记时间
            row7.getCell(4).setCellValue("");

            row7.getCell(5).setCellValue(fwyts);

            row7.getCell(6).setCellValue(zl);

            row7.getCell(8).setCellValue(gyfs);

            //现在办理的房产
            Row row11 = sheet.getRow(10);
            row11.getCell(1).setCellValue(bdcdjxxjgcx.getCxr1());

            row11.getCell(3).setCellValue(bdcdjxxjgcx.getCqzh1());
            //登记时间
            row11.getCell(4).setCellValue(bdcdjxxjgcx.getSqsj());

            String fwyt = mapper.getFwyt(bdcdjxxjgcx.getFwyt1());
            row11.getCell(5).setCellValue(fwyt);


            row11.getCell(6).setCellValue(bdcdjxxjgcx.getZl1());


            row11.getCell(8).setCellValue(bdcdjxxjgcx.getGyfs1());


            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formattedDate = formatter.format(date);
            Row row15 = sheet.getRow(14);
            row15.getCell(6).setCellValue(formattedDate);


            FileOutputStream outputStream = new FileOutputStream(folder+"/" + "bdcdjxxjgcx.xlsx");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
        File file = new File(folder+"/"+ "bdcdjxxjgcx.xlsx");
        return file.getPath();
    }
}
